package com.abbcc.module.admin;

import java.util.Date;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

import com.abbcc.action.BaseAction;
import com.abbcc.models.TdCrk;
import com.abbcc.service.CrkService;
import com.abbcc.util.DateUtil;
import com.abbcc.util.StringUtil;

public class CrkAction extends BaseAction<TdCrk> {
	@Autowired
	private CrkService crkService;
	private String searchKey;
	private String dd1;
	private String dd2;

	public String save() {
		entity.setAddName(getCurrentAdmin() == null ? getCurrentUser()
				.getName() : getCurrentAdmin().getName());
		entity.setAddTime(new Date());
		crkService.save(entity);
		return list();
	}

	public String list() {
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
		if (StringUtil.isNotBlank(searchKey))
			dc.add(Restrictions.like("proType", searchKey, MatchMode.ANYWHERE));
		if (StringUtil.isNotBlank(dd1) && StringUtil.isNotBlank(dd2)) {
			dc.add(Restrictions.between("addTime", DateUtil.strToDate(dd1),
					DateUtil.strToDate(dd2)));
		} else if (StringUtil.isNotBlank(dd1)) {
			dc.add(Restrictions.between("addTime", DateUtil.strToDate(dd1),
					new Date()));
		} else if (StringUtil.isNotBlank(dd2)) {
			dc.add(Restrictions.between("addTime",
					DateUtil.getDateStart(DateUtil.strToDate(dd2)),
					DateUtil.getDateEnd(DateUtil.strToDate(dd2))));
		}
		dc.addOrder(Order.desc("addTime"));
		pageList = crkService.findPageByCriteria(dc, pageSize, startIndex);
		return LIST;
	}

	public String infoUpdate() {
		TdCrk tc = new TdCrk();
		crkService.load(tc, entity.getCrkId());
		if(entity.getJrck()!=0)
			tc.setJrck(entity.getJrck());
		if(entity.getJrrk()!=0)
			tc.setJrrk(entity.getJrrk());
		if(entity.getZrcs()!=0)
			tc.setZrcs(entity.getZrcs());
		if(entity.getDtcs()!=0)
			tc.setDtcs(entity.getDtcs());
		tc.setUpdateTime(new Date());
		crkService.update(tc);
		return list();
	}
	public String del(){
		crkService.load(entity, entity.getCrkId());
		crkService.delete(entity);
		result = "删除成功";
		return list();
	}

	public String getSearchKey() {
		return searchKey;
	}

	public void setSearchKey(String searchKey) {
		this.searchKey = searchKey;
	}

	public String getDd1() {
		return dd1;
	}

	public void setDd1(String dd1) {
		this.dd1 = dd1;
	}

	public String getDd2() {
		return dd2;
	}

	public void setDd2(String dd2) {
		this.dd2 = dd2;
	}

}
